<template>
    <label>
        <input
            :value="modelValue"
            :type="type"
            :placeholder="placeholder"
            :autofocus="autofocus"
            :readonly="readonly"
            :size="size"
            :min="min"
            :max="max"
            :minlength="minlength"
            :maxlength="maxlength"
            @input="
                $emit('update:modelValue', (<HTMLInputElement>$event.target).value, $event), $emit('input', (<HTMLInputElement>$event.target).value, $event)
            "
            @change="$emit('change', (<HTMLInputElement>$event.target).value, $event)"
            @focus="$emit('focus', (isFocus = true), (<HTMLInputElement>$event.target).value, $event)"
            @blur="$emit('blur', (isFocus = false), (<HTMLInputElement>$event.target).value, $event)"
            @keyup.enter="isFocus && $emit('search', (<HTMLInputElement>$event.target).value)"
            :style="{ padding, fontSize, fontStyle, color }"
        />
        <div
            class="icon"
            v-show="iconNullHide ? modelValue : true"
            @click="iconClick()"
            :style="{
                width: iconWidth,
                height: iconHeight,
                top: iconTop,
                left: iconLeft,
                right: iconRight,
                bottom: iconBottom,
            }"
        >
            <slot>
                <blockquote>*</blockquote>
            </slot>
        </div>
    </label>
</template>

<script lang="ts" setup>
import { ref } from "vue";
const props = defineProps({
    modelValue: { type: String },
    type: { type: String, default: "text" },
    placeholder: { type: String },
    autofocus: { type: Boolean },
    readonly: { type: Boolean },
    size: { type: [String, Number] },
    min: { type: [String, Number] },
    max: { type: [String, Number] },
    minlength: { type: [String, Number] },
    maxlength: { type: [String, Number] },
    padding: { type: String },
    fontSize: { type: String },
    fontWeight: { type: String },
    fontStyle: { type: String },
    color: { type: String },
    iconWidth: { type: String, default: "auto" },
    iconHeight: { type: String, default: "auto" },
    iconTop: { type: String },
    iconLeft: { type: String },
    iconRight: { type: String },
    iconBottom: { type: String },
    iconNullHide: { type: Boolean, default: false },
    iconClickToClear: { type: Boolean, default: false },
});
const emits = defineEmits(["update:modelValue", "iconClick", "input", "change", "focus", "blur", "search"]);
const isFocus = ref(false); // 是否在聚焦状态

// 点击图标
const iconClick = () => {
    emits("iconClick", props.modelValue);
    props.iconClickToClear && emits("update:modelValue", "");
};
</script>

<style lang="scss" scoped>
label {
    position: relative;
    display: block;
    transition: all 0.2s;
    input {
        width: 100%;
        height: 100%;
        display: block;
        background-color: transparent;
        box-sizing: border-box;
        transition: all 0.2s;
        outline-color: #ff9e1a;
        outline-width: 2px;
    }
    .icon {
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        cursor: pointer;
        transition: all 0.2s;
    }
}
</style>
